package com.hoshiicloud.user.service.impl.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.organization.ComOrganization;
import com.hoshiicloud.user.entity.organization.ComOrganizationAttach;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.organization.ComOrganizationAttachMapper;
import com.hoshiicloud.user.service.organization.ComOrganizationAttachService;
import com.hoshiicloud.user.service.organization.ComOrganizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机构附件com_organization_attach 服务实现类
 * </p>
 *
 * @author camochen
 * @since 2019-05-27
 */
@Service
public class ComOrganizationAttachServiceImpl extends ServiceImpl<ComOrganizationAttachMapper, ComOrganizationAttach> implements ComOrganizationAttachService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private ComOrganizationService comOrganizationService;

    @Autowired
    private ComOrganizationAttachMapper comOrganizationAttachMapper;

    /**
     * @Author:campchen
     * @Description:
     * @Date:上午 10:45 2019/6/14
     * @Param:
     * @return:
     */
    @Override
    @Cacheable(cacheNames = "ComOrganizationAttach", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "后台用户操作-根据公司信息进行分页查询")
    public IPage<ComOrganizationAttach> getPage(IPage<ComOrganizationAttach> page, QueryWrapper<ComOrganizationAttach> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.organization.entity.ComOrganizationAttach
     * @description: 根据id查询机构附件com_organization_attach
     */
    @Override
    @Cacheable(cacheNames = "ComOrganizationAttach", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "供应商信息-机构附件com_organization_attach -查询机构附件com_organization_attach ")
    public ComOrganizationAttach getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增机构附件com_organization_attach
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationAttach", allEntries = true)
    @Logable(authName = "供应商信息-机构附件com_organization_attach-新增机构附件com_organization_attach")
    public boolean save(ComOrganizationAttach entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        ComOrganization comOrganization = comOrganizationService.getById(entity.getVendorId());
        if (null == comOrganization) {
            throw new ServiceException(CommonError.error("新增失败,采购商或机构信息有误"));
        }
        entity.setDeleted(1);
        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改机构附件com_organization_attach
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationAttach", allEntries = true)
    @Logable(authName = "供应商信息-机构附件com_organization_attach管理-修改机构附件com_organization_attach")
    public boolean updateById(ComOrganizationAttach entity) {
        entity.setModifiedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        List l=new ArrayList();
        return true;
    }

    /**
     *@Author:campchen
     *@Description:
     *@Date:上午 11:25 2019/8/13
     *@Param:
     *@return:
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationAttach", allEntries = true)
    @Logable(authName = "供应商信息-机构附件com_organization_attach管理-批量新增机构附件com_organization_attach")
    @Override
    public boolean insertAttach4ComOrganization(List<ComOrganizationAttach> comOrganizationAttachList) {
        for (int i=0;i<comOrganizationAttachList.size();i++){
            comOrganizationAttachList.get(i).setCreatedTime(LocalDateTime.now());
        }
        return comOrganizationAttachMapper.insertAttach4ComOrganization(comOrganizationAttachList);
    }

    /**
     * @param ids
     * @return boolean
     * @description: 修改机构附件com_organization_attach
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ComOrganizationAttach", allEntries = true)
    @Logable(authName = "供应商信息-机构附件com_organization_attach管理-批量物理删除机构附件com_organization_attach")
    public boolean deletedByIds(List<Long> ids) {
        int row = baseMapper.deleteBatchIds(ids);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }
}
